<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24" >
            <a-form-item label="阈值方案名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['name']" placeholder="请输入阈值方案名称" ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24" >
            <a-form-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select placeholder="请选择是否启用" v-decorator="['enable']">
                <a-select-option value="启用">启用</a-select-option>
                <a-select-option value="禁用">禁用</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="温度阈值方案详情" :key="refKeys[0]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[0]"
          :loading="thresholdPlanDetailTable.loading"
          :columns="thresholdPlanDetailTable.columns"
          :dataSource="thresholdPlanDetailTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true">

          <template v-slot:action1="props">  
            <div >
              <a-select style="width: 175px"  :default-value="props.value"  @change="(value)=> setPoint(value,props)" placeholder="请选择点位">
                <a-select-option v-for="item in point" :value="item" :trigger-change="true">
                  {{ item }}
                </a-select-option>
              </a-select>
            </div> 
          </template>

        </j-editable-table>
      </a-tab-pane>
    </a-tabs>
    <a-row v-if="showFlowSubmitButton" style="text-align: center;width: 100%;margin-top: 16px;"><a-button @click="handleOk">提 交</a-button></a-row>
  </a-spin>
</template>

<script>

  import pick from 'lodash.pick'
  import { getAction } from '@/api/manage'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'ThresholdPlanForm',
    mixins: [JEditableTableMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
        },
        refKeys: ['thresholdPlanDetail', ],
        tableKeys:['thresholdPlanDetail', ],
        activeKey: 'thresholdPlanDetail',
        // 温度阈值方案详情
        thresholdPlanDetailTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '点位名称',
              key: 'point',
              type: FormTypes.slot,
              slotName:'action1',
              width:"200px",
              placeholder: '请选择${title}',
              defaultValue:'',
            },
            {
              title: '高温阈值',
              key: 'high',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '低温阈值',
              key: 'low',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        url: {
          add: "/thresholdPlan/thresholdPlan/add",
          edit: "/thresholdPlan/thresholdPlan/edit",
          queryById: "/thresholdPlan/thresholdPlan/queryById",
          thresholdPlanDetail: {
            list: '/thresholdPlan/thresholdPlan/queryThresholdPlanDetailByMainId'
          },
          getPoint: '/energyMeterMapping/energyMeterMapping/getAll'
        },
        point: [],
      }
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：false流程表单 true普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
      this.getPoint();
    },
    methods: {
      setPoint(value,row){
        console.log(value,row.rowId)
        this.$refs.thresholdPlanDetail.setValues(
          [
            { 
              'rowKey': row.rowId,
              values: {'point': value}
            }
          ]
        ); 
      },
      getPoint(){
        this.point = [];
        getAction(this.url.getPoint).then((res)=>{
          if(res.success){
            res.result.forEach(element => {
              this.point.push(element.point);
            });
            console.log(this.point);
          }
        })
      },
      addBefore(){
        this.form.resetFields()
        this.thresholdPlanDetailTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'createTime','name','enable')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.thresholdPlanDetail.list, params, this.thresholdPlanDetailTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          thresholdPlanDetailList: allValues.tablesValue[0].values,
        }
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          })
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'createTime','name','enable'))
     },

    }
  }
</script>

<style scoped>
</style>